//
//  MineViewController.swift
//  Duyiguqin
//
//  Created by 张昭 on 2024/7/27.
//

import UIKit
import Alamofire

class MineViewController: UIViewController {
    
    override func loadView() {
        let v = UIImageView()
        v.image = UIImage(resource: .pageBg)
        v.contentMode = .scaleAspectFill
        v.isUserInteractionEnabled = true
        self.view = v
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.setNavigationBarHidden(true, animated: true)
        initializeSubviews()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewSafeAreaInsetsDidChange() {
        super.viewSafeAreaInsetsDidChange()
        naviHeaderView.snp.updateConstraints { make in
            make.height.equalTo(view.safeAreaInsets.top + 44)
        }
    }
    
    private lazy var naviHeaderView: UIImageView = {
        let v = UIImageView()
        v.image = UIImage(resource: .pageBg)
        v.contentMode = .scaleAspectFit
        v.clipsToBounds = true
        v.isUserInteractionEnabled = true
        v.addSubview(tLabel)
        tLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.height.equalTo(44)
            make.bottom.equalTo(0)
        }
        return v
    }()
    
    private lazy var tLabel: UILabel = {
        let v = UILabel()
        v.text = "我的"
        v.textColor = UIColor(0x0F0F0F)
        v.font = UIFont.boldSystemFont(ofSize: 18)
        return v
    }()
    
    private lazy var userView: UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 5
        v.layer.masksToBounds = true
        return v
    }()
    
    private lazy var avatarImageView: UIImageView = {
        let v = UIImageView()
        v.layer.cornerRadius = 20
        v.layer.masksToBounds = true
        v.contentMode = .scaleAspectFill
        v.image = UIImage(resource: .iconLogo)
        return v
    }()
    
    private lazy var nicknameLabel: UILabel = {
        let v = UILabel()
        v.font = UIFont.systemFont(ofSize: 16, weight: .bold)
        v.textColor = .black
        v.text = App.shared.userName ?? "点击登录"
        return v
    }()
    
    private lazy var aboutUsView = makeCellView("关于我们", hasLine: true)
    private lazy var contactUsView = makeCellView("联系我们", hasLine: true)
    private lazy var officialAccountsView = makeCellView("公众号", hasLine: true)
    private lazy var loginView = makeCellView("登录", hasLine: true)
    private lazy var logoutView = makeCellView("退出", hasLine: true)
}

extension MineViewController {
    
    private func initializeSubviews() {
        view.backgroundColor = .white
        
        view.addSubview(naviHeaderView)
        naviHeaderView.snp.makeConstraints { make in
            make.height.equalTo(view.safeAreaInsets.top + 44)
            make.left.right.top.equalTo(0)
        }
        
        userView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(onTapCell(_:))))
        view.addSubview(userView)
        userView.snp.makeConstraints { make in
            make.top.equalTo(naviHeaderView.snp.bottom).offset(5)
            make.left.equalTo(10)
            make.right.equalTo(-10)
        }
        
        userView.addSubview(avatarImageView)
        avatarImageView.snp.makeConstraints { make in
            make.size.equalTo(40)
            make.top.equalTo(20)
            make.left.equalTo(10)
            make.bottom.equalTo(-20)
        }
        
        userView.addSubview(nicknameLabel)
        nicknameLabel.snp.makeConstraints { make in
            make.left.equalTo(avatarImageView.snp.right).offset(10)
            make.centerY.equalToSuperview()
            make.right.lessThanOrEqualToSuperview()
        }
        
        aboutUsView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(onTapCell(_:))))
        view.addSubview(aboutUsView)
        aboutUsView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.top.equalTo(userView.snp.bottom).offset(10)
        }
        
        contactUsView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(onTapCell(_:))))
        view.addSubview(contactUsView)
        contactUsView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.top.equalTo(aboutUsView.snp.bottom)
        }
        
        officialAccountsView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(onTapCell(_:))))
        view.addSubview(officialAccountsView)
        officialAccountsView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.top.equalTo(contactUsView.snp.bottom)
        }
        
        loginView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(onTapCell(_:))))
        view.addSubview(loginView)
        loginView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.top.equalTo(officialAccountsView.snp.bottom).offset(20)
        }
        
        logoutView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(onTapCell(_:))))
        view.addSubview(logoutView)
        logoutView.isHidden = true
        logoutView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.top.equalTo(officialAccountsView.snp.bottom).offset(20)
        }
        
        if (App.shared.userName ?? "").isEmpty {
            loginView.isHidden = false
            logoutView.isHidden = true
        } else {
            loginView.isHidden = true
            logoutView.isHidden = false
        }
    }
    
    @objc private func onTapCell(_ sender: UIGestureRecognizer) {
        if sender.view == aboutUsView {
            guard let htmlString = App.shared.aboutInfo else { return }
            let vc = HTMLViewController(HTMLString: htmlString)
            vc.hidesBottomBarWhenPushed = true
            navigationController?.pushViewController(vc, animated: true)
        } else if sender.view == contactUsView {
            guard let htmlString = App.shared.contactInfo else { return }
            let vc = HTMLViewController(HTMLString: htmlString)
            vc.hidesBottomBarWhenPushed = true
            navigationController?.pushViewController(vc, animated: true)
        } else if sender.view == officialAccountsView {
            guard let htmlString = App.shared.officialInfo else { return }
            let vc = HTMLViewController(HTMLString: htmlString)
            vc.hidesBottomBarWhenPushed = true
            navigationController?.pushViewController(vc, animated: true)
        } else if sender.view == loginView || sender.view == logoutView {
            let vc = LoginViewController()
            vc.onLogin = { [weak self] in
                self?.nicknameLabel.text = App.shared.userName ?? ""
                self?.logoutView.isHidden = false
                self?.loginView.isHidden = true
            }
            vc.hidesBottomBarWhenPushed = true
            navigationController?.pushViewController(vc, animated: true)
            App.shared.userName = nil
            App.shared.userAvatar = nil
            self.nicknameLabel.text = "点击登录"
            self.logoutView.isHidden = true
            self.loginView.isHidden = false
        } else if sender.view == userView {
            if (App.shared.userName ?? "").isEmpty {
                let vc = LoginViewController()
                vc.onLogin = { [weak self] in
                    self?.nicknameLabel.text = App.shared.userName ?? ""
                    self?.logoutView.isHidden = false
                    self?.loginView.isHidden = true
                }
                vc.hidesBottomBarWhenPushed = true
                navigationController?.pushViewController(vc, animated: true)
            }
        }
    }
    
    private func makeCellView(_ title: String, hasLine: Bool) -> UIView {
        let bgView = UIView()
        bgView.backgroundColor = .white
        let iconImageView = UIImageView()
        iconImageView.image = UIImage(resource: .iconLogo)
        iconImageView.contentMode = .scaleAspectFill
        iconImageView.clipsToBounds = true
        bgView.addSubview(iconImageView)
        iconImageView.snp.makeConstraints { make in
            make.size.equalTo(20)
            make.top.equalTo(15)
            make.bottom.equalTo(-15)
            make.left.equalTo(20)
        }
        let label = UILabel()
        label.text = title
        bgView.addSubview(label)
        label.font = UIFont.systemFont(ofSize: 15, weight: .regular)
        label.textColor = .black
        label.snp.makeConstraints { make in
            make.centerY.equalTo(iconImageView)
            make.left.equalTo(iconImageView.snp.right).offset(10)
        }
        let nextImageView = UIImageView()
        nextImageView.image = UIImage(resource: .arrowRight)
        bgView.addSubview(nextImageView)
        nextImageView.snp.makeConstraints { make in
            make.right.equalTo(-20)
            make.centerY.equalToSuperview()
        }
        if hasLine {
            let v = UIView()
            v.backgroundColor = UIColor(0xE9E9EA)
            bgView.addSubview(v)
            v.snp.makeConstraints { make in
                make.left.right.bottom.equalTo(0)
                make.height.equalTo(0.5)
            }
        }
        return bgView
    }
}
